﻿using SmugMugApiWrapper;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Threading;
using System.Collections.Generic;
using System.IO;
using System.Diagnostics;
using System;

namespace UnitTestForSmugMugAPIWrapper
{


    /// <summary>
    ///This is a test class for SmugMugApiTest and is intended
    ///to contain all SmugMugApiTest Unit Tests
    ///</summary>
    [TestClass()]
    public class SmugMugApiTest
    {


        private static string _testRootFolder;
        private static string _testUserName;
        private static string _testUserPassword;
        private static string _testImageId = "whatever";
        private static string _testImageKey = "whatever";

        private string _testAlbum = "TestAlbumToDelete";
        private string _testCategory = "Other";

        private TestContext testContextInstance;

        [TestInitialize()]
        public void Initialize()
        {

            UnitTestForSmugMugAPIWrapper.Properties.UnitTestForSmugMugAPIWrapper settings = new UnitTestForSmugMugAPIWrapper.Properties.UnitTestForSmugMugAPIWrapper();

            _testRootFolder = settings.fileLocation;
            _testUserName = settings.username;
            _testUserPassword = settings.password;


           

        }

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        /// <summary>
        ///A test for CreateAlbum
        ///</summary>
        [TestMethod()]
        public void CreateAlbumTest()
        {
            string userName = _testUserName; // TODO: Initialize to an appropriate value
            string userPassword = _testUserPassword; // TODO: Initialize to an appropriate value
            SmugMugApi target = new SmugMugApi(userName, userPassword); // TODO: Initialize to an appropriate value
            CreateAlbumArguments albumArguments = new CreateAlbumArguments(); // TODO: Initialize to an appropriate value
            albumArguments.Name = _testAlbum;
            albumArguments.Public = false;
            albumArguments.Category = "Family";
            albumArguments.Protected = true;
            albumArguments.AllowComments = true;


            SmugMugAlbumInfo actual;
            try
            {
                actual = target.CreateAlbum(albumArguments);
                Assert.IsTrue(actual.AlbumID > 0);
                Assert.IsTrue(actual.AlbumKey.Length > 0);

                Assert.AreEqual(_testAlbum, actual.AlbumName);
            }
            catch (Exception e)
            {
                Assert.Fail("Exception creating album " + e.ToString());
            }


        }


        /// <summary>
        ///A test for PostPicturesToSmugMug
        ///</summary>
        [Priority(0), TestMethod()]
        public void PostPicturesToSmugMugTest()
        {
            string userName = _testUserName;
            string userPassword = _testUserPassword;
            SmugMugApi target = new SmugMugApi(userName, userPassword);
            List<string> fileList = new List<string>();
            fileList.Add(_testRootFolder + "\\images\\test.jpg");

            Assert.IsTrue(File.Exists(_testRootFolder + "\\images\\test.jpg"), "File to upload doesn't exist");

            PostPicturesToSmugMugArguments arguments = new PostPicturesToSmugMugArguments();
            arguments.AlbumName = _testAlbum;

            target.PostPicturesToSmugMug(fileList, arguments);




        }


        /// <summary>
        ///A test for PostPicturesToSmugMug
        ///</summary>
        [Priority(0), TestMethod()]
        public void PostPicturesToSmugMugWithReturnInfoTest()
        {
            string userName = _testUserName;
            string userPassword = _testUserPassword;
            SmugMugApi target = new SmugMugApi(userName, userPassword);
            List<string> fileList = new List<string>();
            fileList.Add(_testRootFolder + "\\images\\test.jpg");

            Assert.IsTrue(File.Exists(_testRootFolder + "\\images\\test.jpg"), "File to upload doesn't exist");

            PostPicturesToSmugMugArguments arguments = new PostPicturesToSmugMugArguments();
            arguments.AlbumName = _testAlbum;

            SmugMugImagesInfo info = target.PostPicturesToSmugMugWithReturnInfo(fileList, arguments);
            Assert.IsTrue(info != null && info.Length > 0);




        }


        /// <summary>
        ///A test for GetSubCategories
        ///</summary>
        [TestMethod()]
        public void GetSubCategoriesTest()
        {
            string userName = _testUserName; // TODO: Initialize to an appropriate value
            string userPassword = _testUserPassword; // TODO: Initialize to an appropriate value
            SmugMugApi target = new SmugMugApi(userName, userPassword); // TODO: Initialize to an appropriate value
            string category = _testCategory; // TODO: Initialize to an appropriate value

            Dictionary<int, string> actual;
            try
            {
                actual = target.GetSubCategories(category);
            }
            catch
            {
                Assert.Fail("Exception in getsubcategoriestest");
            }

        }

        /// <summary>
        ///A test for GetImagesInfo
        ///</summary>
        [TestMethod()]
        public void GetImagesInfoTest()
        {
            string userName = _testUserName; // TODO: Initialize to an appropriate value
            string userPassword = _testUserPassword; // TODO: Initialize to an appropriate value
            SmugMugApi target = new SmugMugApi(userName, userPassword); // TODO: Initialize to an appropriate value
            GetImagesInfoArguments arguments = new GetImagesInfoArguments(); // TODO: Initialize to an appropriate value
            arguments.AlbumName = _testAlbum;

            SmugMugImagesInfo actual;
            actual = target.GetImagesInfo(arguments);

            Assert.IsTrue(actual.Length > 0);

        }

        /// <summary>
        ///A test for GetImages
        ///</summary>
        [TestMethod()]
        public void GetImagesTest()
        {

            //after you upload images, it takes it a while to catch up.
            Thread.Sleep(30 * 1000);



            string userName = _testUserName; // TODO: Initialize to an appropriate value
            string userPassword = _testUserPassword; // TODO: Initialize to an appropriate value
            SmugMugApi target = new SmugMugApi(userName, userPassword); // TODO: Initialize to an appropriate value
            GetImagesArguments arguments = new GetImagesArguments(); // TODO: Initialize to an appropriate value
            arguments.AlbumName = _testAlbum;

            SmugMugImages actual;
            actual = target.GetImages(arguments);
            Assert.IsTrue(actual.Length > 0);

        }

        [TestMethod()]
        public void SetImageInfoTest()
        {
            string userName = _testUserName;
            string userPassword = _testUserPassword;
            SmugMugApi target = new SmugMugApi(userName, userPassword);
            GetImagesInfoArguments arguments = new GetImagesInfoArguments();
            arguments.AlbumName = _testAlbum;

            SmugMugImagesInfo actual;
            actual = target.GetImagesInfo(arguments);
            foreach (SmugMugImageInfo info in actual)
            {
                GetImageInfoArguments args = new GetImageInfoArguments(info.ImageID, info.ImageKey);
                SmugMugImageInfo testImage = target.GetImageInfo(args);

                Assert.AreEqual(testImage.ImageID, info.ImageID);
                Assert.AreEqual(testImage.ImageKey, info.ImageKey);

                SetImageInfoArguments newValues = new SetImageInfoArguments(testImage.ImageID, testImage.ImageKey);

                newValues.Hidden = false;

                string newCaption = DateTime.Now.ToString();
                newValues.Caption = newCaption;
                newValues.Keywords = "whatever";

                SmugMugImageInfo newImageInfo = target.SetImageInfo(newValues);

                Assert.IsFalse(newImageInfo.Hidden);
                Assert.IsTrue(newImageInfo.ImageCaption == newCaption);
                Assert.IsTrue(newImageInfo.KeyWords == "whatever");


            }

        }

        /// <summary>
        ///A test for GetEXIFData
        ///</summary>
        [TestMethod()]
        public void GetEXIFDataTest()
        {
            string userName = _testUserName; // TODO: Initialize to an appropriate value
            string userPassword = _testUserPassword; // TODO: Initialize to an appropriate value
            SmugMugApi target = new SmugMugApi(userName, userPassword); // TODO: Initialize to an appropriate value
            // TODO: Initialize to an appropriate value

            GetImagesInfoArguments i = new GetImagesInfoArguments();
            i.AlbumName = _testAlbum;


            SmugMugImagesInfo ii = target.GetImagesInfo(i);
            string imageKey = "ERROR";
            int imageID = 0;

            Assert.IsTrue(ii.Length > 0);
            foreach (SmugMugImageInfo info in ii)
            {
                Assert.IsInstanceOfType(info, typeof(SmugMugImageInfo));
                imageID = info.ImageID;
                imageKey = info.ImageKey;
            }

            GetEXIFArguments arguments = new GetEXIFArguments(imageID, imageKey);
            SmugMugImageEXIFData actual;

            arguments.ImageID = imageID;

            actual = target.GetEXIFData(arguments);

            if (actual.HasEXIFData)
            {
                Assert.IsTrue(actual.ImageID == imageID);
                Assert.IsTrue(actual.ImageKey == imageKey);
            }

        }

        /// <summary>
        ///A test for GetCategories
        ///</summary>
        ///
        [TestMethod()]
        public void GetCategoriesTest()
        {
            string userName = _testUserName; // TODO: Initialize to an appropriate value
            string userPassword = _testUserPassword; // TODO: Initialize to an appropriate value
            SmugMugApi target = new SmugMugApi(userName, userPassword); // TODO: Initialize to an appropriate value

            Dictionary<int, string> actual;
            actual = target.GetCategories();
            Assert.IsTrue(actual.Count > 0);

        }

        /// <summary>
        ///A test for GetAlbums
        ///</summary>
        ///
        [TestMethod()]
        public void GetAlbumsTest()
        {
            string userName = _testUserName; // TODO: Initialize to an appropriate value
            string userPassword = _testUserPassword; // TODO: Initialize to an appropriate value
            SmugMugApi target = new SmugMugApi(userName, userPassword); // TODO: Initialize to an appropriate value
            List<SmugMugAlbumInfo> expected = new List<SmugMugAlbumInfo>(); // TODO: Initialize to an appropriate value
            List<SmugMugAlbumInfo> actual;
            actual = target.GetAlbums();
            Assert.IsTrue(actual.Count > 0);
            Assert.IsTrue(actual[0].AlbumName.Length > 0);
            Assert.IsTrue(actual[0].Category.CategoryName.Length > 0);


        }

        /// <summary>
        ///A test for DownloadImages
        ///</summary>
        ///
        [TestMethod()]
        public void DownloadImagesTest()
        {
            string userName = _testUserName;
            string userPassword = _testUserPassword;
            SmugMugApi target = new SmugMugApi(userName, userPassword);
            DownloadImagesArguments arguments = new DownloadImagesArguments();
            arguments.AlbumName = _testAlbum;
            arguments.DownloadLocation = _testRootFolder + "\\output";


            //after you upload images, it takes it a while to catch up.
            Thread.Sleep(30 * 1000);



            if (!Directory.Exists(_testRootFolder + "\\output"))
            {
                Directory.CreateDirectory(_testRootFolder + "\\output");
            }
            try
            {
                target.DownloadImages(arguments);
                string[] st = System.IO.Directory.GetFiles(arguments.DownloadLocation);
                Assert.IsTrue(st.Length > 0);
            }
            catch (Exception e)
            {
                Assert.Fail("Error downloading images " + e.ToString());
            }

        }
        [TestMethod()]
        public void DeleteSmugMugImageTest()
        {

            string userName = _testUserName;
            string userPassword = _testUserPassword;

            SmugMugApi target = new SmugMugApi(userName, userPassword);


            GetImagesInfoArguments i = new GetImagesInfoArguments();
            i.AlbumName = _testAlbum;


            SmugMugImagesInfo ii = target.GetImagesInfo(i);


            string imageKey = "ERROR";
            int imageID = 0;

            Assert.IsNotNull(ii);
            Assert.IsTrue(ii.Length > 0);

            foreach (SmugMugImageInfo info in ii)
            {
                Assert.IsInstanceOfType(info, typeof(SmugMugImageInfo));
                imageID = info.ImageID;
                imageKey = info.ImageKey;

            }


            DeleteImageArguments dia = new DeleteImageArguments(imageID.ToString(), imageKey);

            bool returnValue = target.DeleteSmugMugImage(dia);
            if (!returnValue)
                Assert.Fail("Failure deleting image " + imageID + " " + imageKey);
        }






        /// <summary>
        ///A test for DeleteSmugMugAlbum
        ///</summary>
        [TestMethod()]
        public void DeleteSmugMugAlbumTest()
        {
            string userName = _testUserName; // TODO: Initialize to an appropriate value
            string userPassword = _testUserPassword; // TODO: Initialize to an appropriate value
            SmugMugApi target = new SmugMugApi(userName, userPassword); // TODO: Initialize to an appropriate value

            bool returnValue = target.DeleteSmugMugAlbum(_testAlbum);
            if (!returnValue)
            {
                Assert.Fail("Failure deleting " + _testAlbum);
            }


            //now, delete all of the other copies of _testAlbum that you can find.


        }

        [TestMethod()]
        public void SmugMugChangeSettingsTest()
        {

            string userName = _testUserName;
            string userPassword = _testUserPassword;
            SmugMugApi target = new SmugMugApi(userName, userPassword);

            ChangeSettingsArguments csa = new ChangeSettingsArguments(1, "Hey There", "", false);

            try
            {
                target.ChangeSmugmugImageSettings(csa);
            }
            catch
            {
                Assert.Fail("Failure changing image settings");
            }
        }






        /// <summary>
        ///A test for SmugMugApi Constructor
        ///</summary>
        [TestMethod()]
        public void SmugMugApiConstructorTest1()
        {
            string userName = _testUserName; // TODO: Initialize to an appropriate value
            string userPassword = _testUserPassword; // TODO: Initialize to an appropriate value
            try
            {
                SmugMugApi target = new SmugMugApi(userName, userPassword);
            }
            catch
            {
                Assert.Fail("exception in constructor");
            }

        }


    }
}
